<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/18/0018
 * Time: 14:53
 */

namespace app\sports_admin\model;


use think\Model;

class ExpensesReceipts extends Model
{
    protected $pk = 'id';

    protected $table = 'expenses_receipts';

    protected $autoWriteTimestamp = true;

//    public function getStatusAttr($value)
//    {
//        return $value = $value == 1 ? '<span style="color: red">未审核</span>' : '以审核';
//    }
    public function getTypeAttr($value)
    {
        return $value = $value == 1 ? '<span style="color: blue">收入</span>' : '支出';
    }

    /**
     * 获取收支信息
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     * @param $start 备份的开始时间
     * @param $end 备份的结束时间
     * @param $type 收支类型
     * @param $status 收支状态
     */
    public static function getExpensesReceiptsInfo($page,$rows,$sort,$sortOrder,$searchName,$start,$end,$type,$status)
    {
        $result = self::where(function($query)use($searchName,$start,$end,$type,$status){
            if($searchName){
                $query->where('applicant|operator','like',"%{$searchName}%");
            }
            if($start && $end){
                $query->whereTime('create_time','between',["{$start}","{$end}"]);
            }
            if($type){
                $query->where('type',$type);
            }
            if($status){
                $query->where('status',$status);
            }
        })->order("{$sort}","{$sortOrder}")->limit($rows)->page($page)->select();
        $count = self::where(function($query)use($searchName,$start,$end,$type,$status){
            if($searchName){
                $query->where('applicant|operator','like',"%{$searchName}%");
            }
            if($start && $end){
                $query->whereTime('create_time','between',["{$start}","{$end}"]);
            }
            if($type){
                $query->where('type',$type);
            }
            if($status){
                $query->where('status',$status);
            }
        })->count();
        return json(['rows'=>$result,'total'=>$count]);
    }
    /*
     * 查询单个收支单信息
     * @param $id 收支单id
     */
    public static function findInfo($id)
    {
        return self::where('id',$id)->find();
    }
}